package com.css.dzj.zfjg.modules.supervisioncompinfo.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.*;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgBlComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgFbComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.vo.JgTsMsg;
import com.css.dzj.zfjg.modules.supervisioncompinfo.vo.TsXqVo;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 监管投诉信息分办Controller
 * @author xgdou
 */
@RestController
@RequestMapping(value = "/lhjgtsfb/")
public class JgFbComplaintController {
     @Autowired
     JgFbComplaintService jgFbComplaintService;
    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
     @Autowired
     JgBlComplaintService jgBlComplaintService;
    /**
     * 多条件分页查询监管投诉信息
     * @param pageSize
     * @param pageNo
     * @param complaintTerritory
     * @param complaintObject
     * @param fbBureau
     * @param complaintType
     * @param name
     * @param taskState
     * @param receiveStartTime
     * @param receiveEndTime
     * @return
     */
    @GetMapping("listFbComplaininfoEntity")
    public R listJgLhjgCreditAssessEntity(@RequestParam Integer pageSize,
                                          @RequestParam Integer pageNo,
                                          @RequestParam String complaintTerritory,
                                          @RequestParam String complaintObject,
                                          @RequestParam String fbBureau,
                                          @RequestParam String fbDepartment,
                                          @RequestParam String complaintType,
                                          @RequestParam String name,
                                          @RequestParam String taskState,
                                          @RequestParam (required = false) String receiveStartTime,
                                          @RequestParam (required = false) String receiveEndTime
                                          ) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgTsMsg> JgTsjbComplaintInfoEntityQueryResults = jgFbComplaintService.getTsComplainEntity(pageRequest,complaintTerritory,complaintObject,fbBureau,fbDepartment, complaintType, name, taskState,receiveStartTime,receiveEndTime);
            s = JSON.toJSONString(JgTsjbComplaintInfoEntityQueryResults);
            sysAuditService.saveSuccessAudit("多条件分页查询企业投诉信息成功", Constant.ZFJG_LHJGTSFB, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", JgTsjbComplaintInfoEntityQueryResults);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询信息失败", Constant.ZFJG_LHJGTSFB, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     *查看监管投诉详情
     * @param registerCode
     * @return 信息内容详情的vo
     */
    @GetMapping("info")
    public R info(@RequestParam String registerCode) {
        String s = "";
        try {
            //监管投诉信息
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity = jgFbComplaintService.findNo(registerCode);
            //监管任务分办表
            JgFbComplaintEntity jgFbComplaintEntity = jgFbComplaintService.findRegister(registerCode);
            //任务催办表
            JgTsjbUrgeInfoEntity jgTsjbUrgeInfoEntity = jgFbComplaintService.findCode(registerCode);

            //任务经办表
            JgTsjbHandleInfoEntity jgTsjbHandleInfoEntity = jgFbComplaintService.fidByRegister(registerCode);
            //调解信息表
            JgTsjbMediateInfoEntity jgTsjbMediateInfoEntity = jgFbComplaintService.findMediate(registerCode);
            //人员信息表
            //举报人信息表
            JgTsjbUserInfoEntity   jgTsjbUserInfoEntity = jgFbComplaintService.findUser(registerCode);

            //回显的大VO
            TsXqVo tsXqVo = new TsXqVo();
            tsXqVo.setJgTsjbComplaintInfoEntity(jgTsjbComplaintInfoEntity);
            tsXqVo.setJgFbComplaintEntity(jgFbComplaintEntity);
            tsXqVo.setJgTsjbUrgeInfoEntity(jgTsjbUrgeInfoEntity);
            tsXqVo.setJgTsjbHandleInfoEntity(jgTsjbHandleInfoEntity);
            tsXqVo.setJgTsjbUserInfoEntity(jgTsjbUserInfoEntity);
            tsXqVo.setJgTsjbMediateInfoEntity(jgTsjbMediateInfoEntity);
            s = JSON.toJSONString(tsXqVo);
            sysAuditService.saveSuccessAudit("通过任务编码获取任务详情成功", Constant.ZFJG_LHJGTSFB, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("tsXqVo", tsXqVo);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询信息失败", Constant.ZFJG_LHJGTSFB, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }
    /**
     * 获取任务分办页面
     * @param registerCode
     * @return 监管投诉信息表  投诉人员信息表
     */
    @GetMapping("getfbinfo")
    public R getfbinfo(@RequestParam String registerCode) {
        String s = "";
        try{
            //监管投诉信息
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity = jgFbComplaintService.findNo(registerCode);
            //投诉人员信息
            JgTsjbUserInfoEntity jgTsjbUserInfoEntity = jgFbComplaintService.findUser(registerCode);
            TsXqVo tsXqVo2 = new TsXqVo();
            tsXqVo2.setJgTsjbComplaintInfoEntity(jgTsjbComplaintInfoEntity);
            tsXqVo2.setJgTsjbUserInfoEntity(jgTsjbUserInfoEntity);
            s = JSON.toJSONString(tsXqVo2);
            sysAuditService.saveSuccessAudit("查询详情成功", Constant.ZFJG_LHJGTSFB, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("tsXqVo2", tsXqVo2);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询信息失败", Constant.ZFJG_LHJGTSFB, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 监管投诉分办保存
     */
    @PostMapping("save")
    public R save(@RequestBody TsXqVo tsXqVo, HttpServletRequest request) {
        String s = "";
        try{
            //将分办信息入库
            JgFbComplaintEntity jgFbComplaintEntity = tsXqVo.getJgFbComplaintEntity();
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity = tsXqVo.getJgTsjbComplaintInfoEntity();
            JgFbComplaintEntity oldJgTaskinfoEntity = jgFbComplaintService.findRegister(jgTsjbComplaintInfoEntity.getRegisterCode());
            if(oldJgTaskinfoEntity!=null){
                BeanUtil.copyBeanNotEmptyToBean(jgFbComplaintEntity, oldJgTaskinfoEntity);
                jgFbComplaintService.update(jgFbComplaintEntity,request);
            }else{
                jgFbComplaintEntity.setRegisterCode(jgTsjbComplaintInfoEntity.getRegisterCode());
                jgFbComplaintService.add(jgFbComplaintEntity, request);
            }
            //将监管投诉信息状态改为已分办
            jgFbComplaintService.update(jgTsjbComplaintInfoEntity,request);
            s = JSON.toJSONString(jgTsjbComplaintInfoEntity);
            sysAuditService.saveSuccessAudit("分办成功", Constant.ZFJG_LHJGTSFB, Constant.PT, Constant.ZFJG_FBTASK, s, request);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("分办失败", Constant.ZFJG_LHJGTSFB, Constant.YZ, Constant.ZFJG_FBTASK, s, e, request);
            return R.error("提交失败");
        }

    }
    /**
     * 投诉信息反馈 taskState状态改为106
     */
    @GetMapping("tsfk")
    public R tsfk(@RequestParam String registerCode,HttpServletRequest request) {
        String s = "";
        try{
            //修改反馈信息表
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity = jgFbComplaintService.findNo(registerCode);
            String flag = "106";
            jgBlComplaintService.updatetsjb(jgTsjbComplaintInfoEntity,request,flag);
            s = JSON.toJSONString(jgTsjbComplaintInfoEntity);
            sysAuditService.saveSuccessAudit("反馈成功", Constant.ZFJG_LHJGTSFB, Constant.PT, Constant.ZFJG_FEEDBACK, s, request);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("反馈失败", Constant.ZFJG_LHJGTSFB, Constant.YZ, Constant.ZFJG_FEEDBACK, s, e, request);
            return R.error("提交失败");
        }

    }
    /**
     * 投诉信息任务结束 taskState状态改为105
     */
    @GetMapping("tsdf")
    public R tsdf(@RequestParam String registerCode,HttpServletRequest request) {
        String s = "";
        try{
            //修改反馈信息表
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity = jgFbComplaintService.findNo(registerCode);
            String flag = "105";
            jgBlComplaintService.updatetsjb(jgTsjbComplaintInfoEntity,request,flag);
            s = JSON.toJSONString(jgTsjbComplaintInfoEntity);
            sysAuditService.saveSuccessAudit("任务结束成功", Constant.ZFJG_LHJGTSFB, Constant.PT, Constant.ZFJG_ENDTASK, s, request);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("任务结束失败", Constant.ZFJG_LHJGTSFB, Constant.YZ, Constant.ZFJG_ENDTASK, s, e, request);
            return R.error("提交失败");
        }

    }
}
